/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BALocations.m
//  redia-bapps-ios
//
//  Created by Brian Vestergaard Danielsen on 12/03/14.
//  Copyright (c) 2014 Redia A/S. All rights reserved.
//

#import "BAMaterialLocations.h"


@implementation BAMaterialLocations

+ (id)objectWithJSONObject:(NSDictionary *)jsonObj
{
    BAMaterialLocations *obj = [BAMaterialLocations new];
    
    obj.locations = [JSONEncodingHelperMethods parseArrayOfClass:[BAMaterialLocation class] withKey:@"locations" fromJsonObject:jsonObj];
    obj.subLocations = [JSONEncodingHelperMethods parseArrayOfClass:[BAMaterialLocation class] withKey:@"subLocations" fromJsonObject:jsonObj];
    obj.departments = [JSONEncodingHelperMethods parseArrayOfClass:[BAMaterialLocation class] withKey:@"departments" fromJsonObject:jsonObj];
    obj.collections = [JSONEncodingHelperMethods parseArrayOfClass:[BAMaterialLocation class] withKey:@"collections" fromJsonObject:jsonObj];
    obj.reservationBranches =[JSONEncodingHelperMethods parseArrayOfClass:[BABranch class] withKey:@"reservationBranches" fromJsonObject:jsonObj];
    obj.branches = [JSONEncodingHelperMethods parseArrayOfClass:[BABranch class] withKey:@"branches" fromJsonObject:jsonObj];
    
    return obj;
}

-(void)setLocations:(NSArray *)locations{
    _locations = locations;
    NSMutableDictionary* locationsDict = [NSMutableDictionary new];
    for (BAMaterialLocation* location in locations) {
        [locationsDict setObject:location forKey:location.code];
    }
    _locationsDict = [NSDictionary dictionaryWithDictionary:locationsDict];
}

-(void)setSubLocations:(NSArray *)subLocations{
    _subLocations = subLocations;
    NSMutableDictionary* subLocationsDict = [NSMutableDictionary new];
    for (BAMaterialLocation* subLocation in subLocations) {
        [subLocationsDict setObject:subLocation forKey:subLocation.code];
    }
    _subLocationsDict = [NSDictionary dictionaryWithDictionary:subLocationsDict];
}

-(void)setDepartments:(NSArray *)departments{
    _departments = departments;
    NSMutableDictionary* departmentsDict = [NSMutableDictionary new];
    for (BAMaterialLocation* department in departments) {
        [departmentsDict setObject:department forKey:department.code];
    }
    _departmentsDict = [NSDictionary dictionaryWithDictionary:departmentsDict];
}

-(void)setCollections:(NSArray *)collections{
    _collections = collections;
    NSMutableDictionary* collectionsDict = [NSMutableDictionary new];
    for (BAMaterialLocation* collection in collections) {
        [collectionsDict setObject:collection forKey:collection.code];
    }
    _collectionsDict = [NSDictionary dictionaryWithDictionary:collectionsDict];
}

-(void)setReservationBranches:(NSArray *)reservationBranches{
    _reservationBranches = reservationBranches;
    NSMutableDictionary* reservationBranchesDict = [NSMutableDictionary new];
    for (BABranch* branch in reservationBranches) {
        [reservationBranchesDict setObject:branch forKey:branch.code];
    }
    _reservationBranchesDict = [NSDictionary dictionaryWithDictionary:reservationBranchesDict];
}

-(void)setBranches:(NSArray *)branches{
    _branches = branches;
    NSMutableDictionary* branchesDict = [NSMutableDictionary new];
    for (BABranch* branch in branches) {
        [branchesDict setObject:branch forKey:branch.code];
    }
    _branchesDict = [NSDictionary dictionaryWithDictionary:branchesDict];
}

- (NSDictionary *)toJSONObject {
    
    NSMutableArray* locationsJson = [NSMutableArray new];
    for (BAMaterialLocation* location in self.locations) {
        [locationsJson addObject:[location toJSONObject]];
    }
    
    NSMutableArray* subLocationsJson = [NSMutableArray new];
    for (BAMaterialLocation* location in self.subLocations) {
        [subLocationsJson addObject:[location toJSONObject]];
    }
    
    NSMutableArray* departmentsJson = [NSMutableArray new];
    for (BAMaterialLocation* location in self.departments) {
        [departmentsJson addObject:[location toJSONObject]];
    }
    
    NSMutableArray* collectionsJson = [NSMutableArray new];
    for (BAMaterialLocation* location in self.collections) {
        [collectionsJson addObject:[location toJSONObject]];
    }
    
    NSMutableArray* reservationBranchesJson = [NSMutableArray new];
    for (BABranch* branch in self.reservationBranches) {
        [reservationBranchesJson addObject:[branch toJSONObject]];
    }
    
    NSMutableArray* branchesJson = [NSMutableArray new];
    for (BABranch* branch in self.branches) {
        [branchesJson addObject:[branch toJSONObject]];
    }
    
   
    
    NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:@{
                                                                                  @"locations" : locationsJson,
                                                                                  @"subLocations" : subLocationsJson,
                                                                                  @"departments" : departmentsJson,
                                                                                  @"collections" : collectionsJson,
                                                                                  @"reservationBranches" : reservationBranchesJson,
                                                                                  @"branches" : branchesJson
                                                                                  }];
    return result;
}


@end
